<?php

class Cashback_configAction extends CommonAction
{
	
	//全返列表
	public function index(){
		
		parent::_list();
		
		
		$list = $this->get('list');
		
		$pdb = M('Product');
		foreach($list as $k => $v){
			$product = $pdb -> where(array('site_id' => $this->siteid, 'id' => $v['product_id'])) -> find();
			$list[$k]['img_url'] = $product['img_url'];
			$list[$k]['reprice'] = $product['price'];
			
		}
		$this -> assign('list', $list);
		$this -> display();
	}
	
	
	public function edit(){
		$relatelist = array();
		$pid = $this->dao -> where(array('id' => $this->_get('id', 'int'))) -> getField('product_id');
		$relatelist[] = M('Product') -> where(array('id' => array('in', $pid))) -> find();
		$this->assign('relatelist', $relatelist);
		parent::edit();
	}
	
	
	public function update(){
		$_POST['product_id'] = $this->_post('relateids');
		parent::update();
	}
	public function _list(){
		//计算总页数
		$count=$this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->count();
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$p=new Page($count,$this->pagesize);
		$this->list = $this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name, c.name as cate_name')->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
'.$p->listRows)->select();
        $this->pages=$p->show();
	}
	
	function selectProduct(){
		$this->dao = D('Product');
		$this->listmap="1 and n.site_id=".$this->siteid;
		$this->listorder="sort,id";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords; 
		}
		$relateids = $this->dao -> where(array('id' => $this->_get('product_id'), 'site_id' => $this->siteid)) -> getField('relateids');
		$this->selectedid = explode(',', $relateids);
		//dump($selectedid);
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		$this->display();
	}
	
	
	public function log(){
		$product_id = $this->_get('product_id');
		$db = M('Cashback_count');
		$count = $db -> where(array('product_id' => $product_id)) -> count();
		import ('ORG.Util.Page');
		$p=new Page($count,20);
		$this->list = $db ->alias('c')->join('left join __PRODUCT__ p ON p.id= c.product_id') -> join("left join __MEMBER__ as m on m.id = c.member_id") ->Field('c.total,c.last_money,c.rest,c.rate,c.member_id,p.title,p.price,p.img_url as pimg_url,m.img_url,m.nickname')->where(array('c.product_id' => $product_id))->order('c.member_id')->limit($p->firstRow.',
'.$p->listRows)->select();
		
		$this->assign('list', $this->list);
		$this->assign('pages', $p->show());
		
		$this->display();

	}
}